package com.fingard.dsp.bank.directbank.crbc01;

import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

public class CRBC01SinglePay extends CRBC01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        if (p_req1908.singleDetail.sameBnk.equals("0")) {
            //单笔跨行付款300002
            return d_pay300002(p_req1908);
        } else {
            //单笔同行付款300001
            return d_pay300001(p_req1908);
        }
    }

    //单笔跨行付款300002
    public RetAtsSingle d_pay300002(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CRBC01Header bankHeader = new CRBC01Header();
            bankHeader.tranCode = "300002";
            bankHeader.tranTime = new Date();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("BODY");
            xmlWriter.writeElementString("PAYERACC", p_req1908.singleDetail.payAct);//付款账号
            xmlWriter.writeElementString("PAYERNAME", p_req1908.singleDetail.payName);//付款人户名
            xmlWriter.writeElementString("CURRENCY", convertCurToBank(p_req1908.singleDetail.   payCur));//币种
            xmlWriter.writeElementString("PAYERACCBANK", p_req1908.singleDetail.payBankCode);//付款人开户行
            xmlWriter.writeElementString("CUSTMERTYPE", "2");//对公对私标志 1-对私2-对公 目前只能对公
            xmlWriter.writeElementString("PAYEEACC", p_req1908.singleDetail.recAct);//收款账号
            xmlWriter.writeElementString("PAYEENAME", p_req1908.singleDetail.recName);//收款方账户名
            xmlWriter.writeElementString("PAYEEACCBANKNAME", p_req1908.singleDetail.cnapsName);//收款方开户行名称
            xmlWriter.writeElementString("PAYEEACCBANK", p_req1908.singleDetail.cnapsCode);//收款行开户行号
            xmlWriter.writeElementString("AMOUNT", p_req1908.singleDetail.payAmount);//金额
            xmlWriter.writeElementString("TRANSFERTYPE", p_req1908.singleDetail.paySpd.equals("1") ? "1" : "0");//支付类型 0-普通；1-加急
            xmlWriter.writeElementString("PAYUSE", p_req1908.singleDetail.purpose);//用途
            xmlWriter.writeElementString("REMARK", p_req1908.singleDetail.memo);//备注
            xmlWriter.writeElementString("SERIALNO", p_req1908.singleDetail.askNumber);//ERP流水号
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRespCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                    String tmpRespInfo = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                    if (tmpRespCode.equals("000000")) {
                        String status = tmpBkReader.getFirstText("/CRBANK/BODY/STATUS");
                        String retmsg = tmpBkReader.getFirstText("/CRBANK/BODY/RETMSG");
                        tmpRetAtsDetail.respCode = "0";
                        tmpRetAtsDetail.respInfo = retmsg;
                        tmpRetAtsDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                        tmpRetAtsDetail.transState = status;
                        tmpRetAtsDetail.payInfoCode = tmpRetAtsDetail.transState;
                    } else if (tmpRespCode.equals("E19000") || tmpRespCode.equals("E40001") || tmpRespCode.equals("E40002")) {
                        //E19000、E40001、E40002：有可能是前置机接收银行系统返回超时，前置机系统处理异常等问题，
                        // 此时不确定交易状态，对于转账类交易可以认为这笔交易为可疑状态
                        tmpRetAtsDetail.respCode = "0";
                        tmpRetAtsDetail.respInfo = tmpRespInfo;
                        tmpRetAtsDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                        tmpRetAtsDetail.transState = tmpRespCode;
                        tmpRetAtsDetail.payInfoCode = tmpRetAtsDetail.transState;
                    } else {
                        //其余非000000的返回码，均可确定为交易失败，并且会返回失败原因。
                        tmpRetAtsDetail.respCode = "-1";
                        tmpRetAtsDetail.respInfo = "错误代码：" + tmpRespCode + "错误信息：" + tmpRespInfo;
                    }
                } else {
                    tmpRetAtsDetail.respCode = "-1";
                    tmpRetAtsDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetAtsDetail.respCode = "-1";
                tmpRetAtsDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRetAtsDetail.respCode = "-1";
            tmpRetAtsDetail.respInfo = "跨行付款出现异常！";
            WriteBankLogLn("跨行付款出现异常,异常信息：" + e.getMessage());
        }
        retAts.writeRetDetail(tmpRetAtsDetail);
        return retAts;
    }

    //单笔同行付款300001
    public RetAtsSingle d_pay300001(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CRBC01Header bankHeader = new CRBC01Header();
            bankHeader.tranCode = "300001";
            bankHeader.tranTime = new Date();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("BODY");
            xmlWriter.writeElementString("BASE_ACCT_NO", p_req1908.singleDetail.payAct);//账号
            xmlWriter.writeElementString("ACCT_NAME", p_req1908.singleDetail.payName);//账户名称
            xmlWriter.writeElementString("CCY", convertCurToBank(p_req1908.singleDetail.payCur));//币种
            xmlWriter.writeElementString("NARRATIVE", p_req1908.singleDetail.abstractATS);//交易说明(存入对帐码)
            xmlWriter.writeElementString("SEQ_NO", p_req1908.singleDetail.askNumber);//ERP流水号
            xmlWriter.writeElementString("TRAN_AMT", p_req1908.singleDetail.payAmount);//交易金额
            xmlWriter.writeElementString("CP_ACCT_NO", p_req1908.singleDetail.recAct);//交易对手账号
            xmlWriter.writeElementString("CP_ACCT_NAME", p_req1908.singleDetail.recName);//交易对手账户名称
            xmlWriter.writeElementString("CHANNEL_FLAG", "");//是否E润通客户 是：1；否：为空或者为0
            xmlWriter.writeElementString("PAYMENT_BILL", "");//付款单号 当CHANNEL_FLAG为1时必填
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRespCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                    String tmpRespInfo = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                    if (tmpRespCode.equals("000000")) {
                        String status = tmpBkReader.getFirstText("/CRBANK/BODY/STATUS");
                        String retmsg = tmpBkReader.getFirstText("/CRBANK/BODY/RETMSG");
                        tmpRetAtsDetail.respCode = "0";
                        tmpRetAtsDetail.respInfo = retmsg;
                        tmpRetAtsDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                        tmpRetAtsDetail.transState = status;
                        tmpRetAtsDetail.payInfoCode = tmpRetAtsDetail.transState;
                    } else if (tmpRespCode.equals("E19000") || tmpRespCode.equals("E40001") || tmpRespCode.equals("E40002")) {
                        //E19000、E40001、E40002：有可能是前置机接收银行系统返回超时，前置机系统处理异常等问题，
                        // 此时不确定交易状态，对于转账类交易可以认为这笔交易为可疑状态
                        tmpRetAtsDetail.respCode = "0";
                        tmpRetAtsDetail.respInfo = tmpRespInfo;
                        tmpRetAtsDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                        tmpRetAtsDetail.transState = tmpRespCode;
                        tmpRetAtsDetail.payInfoCode = tmpRetAtsDetail.transState;
                    } else {
                        //其余非000000的返回码，均可确定为交易失败，并且会返回失败原因。
                        tmpRetAtsDetail.respCode = "-1";
                        tmpRetAtsDetail.respInfo = "错误代码：" + tmpRespCode + "错误信息：" + tmpRespInfo;
                    }
                } else {
                    tmpRetAtsDetail.respCode = "-1";
                    tmpRetAtsDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetAtsDetail.respCode = "-1";
                tmpRetAtsDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRetAtsDetail.respCode = "-1";
            tmpRetAtsDetail.respInfo = "同行付款出现异常！";
            WriteBankLogLn("同行付款出现异常,异常信息：" + e.getMessage());
        }
        retAts.writeRetDetail(tmpRetAtsDetail);
        return retAts;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            CRBC01Header bankHeader = new CRBC01Header();
            bankHeader.tranCode = "400001";
            bankHeader.tranTime = new Date();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("BODY");
            xmlWriter.writeElementString("SERIALNO", p_req1944.askNumber);//ERP交易流水号
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRespCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                    String tmpRespInfo = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                    if (tmpRespCode.equals("000000")) {
                        String status = tmpBkReader.getFirstText("/CRBANK/BODY/TRAN_STATE");
                        String retmsg = tmpBkReader.getFirstText("/CRBANK/BODY/STATE_DESC");
                        tmpRetAtsDetail.respCode = "0";
                        tmpRetAtsDetail.respInfo = retmsg;
                        tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
                        tmpRetAtsDetail.transState = status;
                        tmpRetAtsDetail.payInfoCode = tmpRetAtsDetail.transState;
                    } else if (tmpRespCode.equals("E19000") || tmpRespCode.equals("E40001") || tmpRespCode.equals("E40002")) {
                        //E19000、E40001、E40002：有可能是前置机接收银行系统返回超时，前置机系统处理异常等问题，
                        // 此时不确定交易状态，对于转账类交易可以认为这笔交易为可疑状态
                        tmpRetAtsDetail.respCode = "0";
                        tmpRetAtsDetail.respInfo = tmpRespInfo;
                        tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
                        tmpRetAtsDetail.transState = tmpRespCode;
                        tmpRetAtsDetail.payInfoCode = tmpRetAtsDetail.transState;
                    } else {
                        //其余非000000的返回码，均可确定为交易失败，并且会返回失败原因。
                        tmpRetAtsDetail.respCode = "-1";
                        tmpRetAtsDetail.respInfo = "错误代码：" + tmpRespCode + "错误信息：" + tmpRespInfo;
                    }
                } else {
                    tmpRetAtsDetail.respCode = "-1";
                    tmpRetAtsDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetAtsDetail.respCode = "-1";
                tmpRetAtsDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRetAtsDetail.respCode = "-1";
            tmpRetAtsDetail.respInfo = "查询交易结果出现异常！";
            WriteBankLogLn("查询交易结果出现异常,异常信息：" + e.getMessage());
            e.printStackTrace();
        }
        retAts.writeRetDetail(tmpRetAtsDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
